package hot;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class wuliu {
//    https://leetcode.cn/problems/merge-intervals/description/?envType=study-plan-v2&envId=top-100-liked
    class Solution {
        public int[][] merge(int[][] intervals) {
            //先对数组进行左端点从小到大的排序
            Arrays.sort(intervals,(p, q)->(p[0]-q[0]));
            List<int[]> list=new ArrayList<>();
            for(int[] p:intervals){
                int m=list.size();
                if(m>0&&p[0]<=list.get(m-1)[1]){
                    list.get(m-1)[1]=Math.max(p[1],list.get(m-1)[1]);
                }else{
                    list.add(p);
                }
            }
            return list.toArray(new int[list.size()][]);

        }
    }
}
